﻿using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Yxinmt.Controls
{
    public class AxAcroPDF
    {
        static string _OcxState = "AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACFTeXN0ZW0uV2luZG93cy5Gb3Jtcy5BeEhvc3QrU3RhdGUBAAAABERhdGEHAgIAAAAJAwAAAA8DAAAAIQAAAAIBAAAAAQAAAAAAAAAAAAAAAAwAAAAADAAA2BMAANgTAAAL";

        public AxAcroPDFLib.AxAcroPDF AcroPDF { get; set; }

        public Dictionary<string, object> _Options { get; set; }

        public AxAcroPDF(Dictionary<string, object> options)
        {
            _Options = options;
        }

        public void OpenFile(string fileName)
        {
            AcroPDF.src = fileName;
        }

        public void InitAxFramerControl(System.Windows.Forms.Integration.WindowsFormsHost windowsFormsHost)
        {
            AcroPDF = new AxAcroPDFLib.AxAcroPDF();
            ((System.ComponentModel.ISupportInitialize)(AcroPDF)).BeginInit();

            AcroPDF.Enabled = true;
            AcroPDF.Name = _Options.GetStringValue("Name");
            AcroPDF.Dock = System.Windows.Forms.DockStyle.Fill;
            AcroPDF.OcxState = GetAxHostState();

            windowsFormsHost.Child = AcroPDF;

            ((System.ComponentModel.ISupportInitialize)(AcroPDF)).EndInit();
        }

        private System.Windows.Forms.AxHost.State GetAxHostState()
        {
            byte[] bs = Convert.FromBase64String(_OcxState);

            MemoryStream ms = new System.IO.MemoryStream(bs);
            BinaryFormatter bf = new BinaryFormatter();

            return (System.Windows.Forms.AxHost.State)bf.Deserialize(ms);
        }
    }
}
